<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules"  :inline="true"  size="mini">
      <el-row>
         <el-col :span="6">
          <el-form-item label="起始条码位" >
            <el-input v-model="form.codeLength" :disabled="true" class="input_w"></el-input>
        </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="条码起始内容" >
            <el-input v-model="form.codePre" :disabled="true" class="input_w"></el-input>
          </el-form-item>
         </el-col>
      </el-row>
      <el-row>
         <el-col :span="6">
          <el-form-item label="条码中间位" >
             <el-input v-model="form.codeLength" :disabled="true" class="input_w"></el-input>
        </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="中间位生成规则" >
            <el-select v-model="form.codeMiddleRule" placeholder="请选择生成规则" class="input_w">
              <el-option label="随机" value="0"></el-option>
              <el-option label="递增" value="1"></el-option>
           </el-select>
          </el-form-item>
         </el-col>
        <el-col :span="10">
         <el-form-item label="规则包含内容" >
            <el-checkbox-group v-model="form.codeMiddleContent">
              <el-checkbox label="数字(0-9)" name="codeMiddleContent"></el-checkbox>
              <el-checkbox label="大写字母(A_Z)" name="codeMiddleContent"></el-checkbox>
              <el-checkbox label="小写字母(a-z)" name="codeMiddleContent"></el-checkbox>
            </el-checkbox-group>
        </el-form-item>
         </el-col>
      </el-row>

      <el-row>
         <el-col :span="6">
          <el-form-item label="条码结束位" >
             <el-input v-model="form.codeLength" :disabled="true" class="input_w"></el-input>
        </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结束位生成规则" >
            <el-select v-model="form.codeEndRule" placeholder="请选择生成规则" class="input_w">
              <el-option label="随机" value="0"></el-option>
              <el-option label="递增" value="1"></el-option>
           </el-select>
          </el-form-item>
         </el-col>
         <el-col :span="10">
         <el-form-item label="规则包含内容">
            <el-checkbox-group v-model="form.codeEndContent">
              <el-checkbox label="数字(0-9)" name="codeEndContent"></el-checkbox>
              <el-checkbox label="大写字母(A_Z)" name="codeEndContent"></el-checkbox>
              <el-checkbox label="小写字母(a-z)" name="codeEndContent"></el-checkbox>
            </el-checkbox-group>
        </el-form-item>
         </el-col>
      </el-row>


      <el-row>
         <el-col :span="6">
          <el-form-item label="加密算法" >
            <el-select v-model="form.encryptionType" placeholder="请选择加密类型" class="input_w">
              <el-option label="DES" value="0"></el-option>
              <el-option label="AES" value="1"></el-option>
              <el-option label="XOR" value="2"></el-option>
           </el-select>
        </el-form-item>
        </el-col>
        <el-col :span="6">
           <el-form-item label="生成条码数量" prop="codeNumber" >
              <template>
              <el-input-number  v-model="form.codeNumber" :min="0" :max="100000" label="生成条码数量" size="small" class="input_w"></el-input-number>
             </template>
          </el-form-item>
         </el-col>
           <el-col :span="10">
        <el-form-item label="生成条码颜色" prop="color">
        <template>
          <el-select v-model="form.color" placeholder="请选择" class="input_w">
            <el-option
              v-for="item in colorOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
             </el-option>
            </el-select>
        </template>
        </el-form-item>
         </el-col>
      </el-row>

      <el-form-item>

        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['labdata:twoDimensionalCode:edit']"
        >批量删除</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetform">重 置</el-button>
        <el-button type="primary"  size="mini" @click="generate" v-hasPermi="['labdata:twoDimensionalCode:edit']">生成二维码</el-button>
      </el-form-item>
    </el-form>

   <el-row :gutter="10" class="mb8" v-show="false">
      <el-col :span="1.5">
      </el-col>
      <el-col :span="1.5" >
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['labdata:twoDimensionalCode:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="twoDimensionalCodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
       <el-table-column label="二维码" align="center" prop="id" >
      <template slot-scope="scope">
        <el-popover
            placement="right"
            width="400"
            trigger="hover">
            <img :src=bindImage(scope.row.id) class="imgStyle"/>
            <img slot="reference" :src=bindImage(scope.row.id) style="max-height: 50px;max-width: 450px">
        </el-popover>
    </template>

    </el-table-column>
      <el-table-column label="二维码含义" align="center" prop="code" width="400" />
      <el-table-column label="颜色" align="center" prop="color" >
         <template slot-scope="scope">
        {{getColorType(scope.row.color)}}
         </template>
      </el-table-column>
      <el-table-column label="是否导出" align="center" prop="isExport" >
        <template slot-scope="scope">
       {{getisExportType(scope.row.isExport)}}
         </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
           size="mini"
            type="text"
            icon="el-icon-edit"
           @click="handleView(scope.row)">查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['labdata:twoDimensionalCode:edit']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="查看二维码"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      center
      width="350px"
      :modal-append-to-body="false">
      <span>
       <img slot="reference" :src="fileUrl" style="max-height: 350px;max-width: 350px">
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { listTwoDimensionalCode, getTwoDimensionalCode, delTwoDimensionalCode, addTwoDimensionalCode, updateTwoDimensionalCode, exportTwoDimensionalCode } from "@/api/labdata/twoDimensionalCode";
import { formatNowDate } from "@/utils/index";

export default {
  name: "TwoDimensionalCodeGeneration",
  data() {
        // 表单校验规则
        var validatePass = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('生成条码数量'));
          } else if (value === 0) {
            callback(new Error('生成条码数量不能为0'));
          }else{
             callback();
          }
        };

    return {
      host:process.env.VUE_APP_BASE_API,
      fileUrl: "",
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 二维码表格数据
      twoDimensionalCodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
       // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isExport:'0'
      },
      // 表单参数
      form: {
        codeNumber:0,
        //定义位数长度
        codeLength:10,
        codePre:"",
        codeMiddleContent:[],
        codeEndContent:[]


      },
      // 表单校验
      rules: {
        codeNumber: [
          { required: true, validator: validatePass, trigger: "blur" }
        ],
         color: [
          { required: true,  message: '请选择条码颜色', trigger: "blur" }
        ]
      },
      // 条形码颜色
       colorOptions: [{
          value: '0',
          label: '红色'
        }, {
          value: '1',
          label: '蓝色'
        }, {
          value: '3',
          label: '黑色'
        }],
        // 条形码是否导出
       isExportOptions: [{
          value: '0',
          label: '否'
        }, {
          value: '1',
          label: '是'
        }],
        // 条形码是否绑定物体
       isBindingOptions: [{
          value: '0',
          label: '否'
        }, {
          value: '1',
          label: '是'
        }],
    };
  },
  created() {
    this.getList();
    this.form.codePre = formatNowDate();

  },
  methods: {
    /** 查询二维码列表 */
    getList() {
      this.loading = true;
      listTwoDimensionalCode(this.queryParams).then(response => {
        this.twoDimensionalCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 重置按钮操作 */
    resetform() {
        this.form = {
        codeNumber: null,
        color: null
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    generate() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            addTwoDimensionalCode(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },

   /** 取消查看按钮操作 */
    handleClose(done) {
        this.dialogVisible = false
      },

    /** 查看按钮操作 */
    handleView(row) {
      this.dialogVisible = true;
      this.fileUrl = this.host+'/labdata/twoDimensionalCode/imageDowload/image?id='+row.id
    },

    bindImage(value){
      return this.host+'/labdata/twoDimensionalCode/imageDowload/image?id='+value
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除二维码编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTwoDimensionalCode(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有二维码数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTwoDimensionalCode(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    //使用插槽
	getColorType(e) {
      for (var i = 0; i < this.colorOptions.length; i++) {
        if (this.colorOptions[i].value == e) {
          return this.colorOptions[i].label;
        }
      }
    },

    //使用插槽
	getisExportType(e) {
      for (var i = 0; i < this.isExportOptions.length; i++) {
        if (this.isExportOptions[i].value == e) {
          return this.isExportOptions[i].label;
        }
      }
    },

    //使用插槽
	getisBingdingType(e) {
      for (var i = 0; i < this.isBindingOptions.length; i++) {
        if (this.isBindingOptions[i].value == e) {
          return this.isBindingOptions[i].label;
        }
      }
    },

  }
};
</script>
<style scoped lang="scss">
.input_w{
  width: 120px;
}
</style>
